Imports System
Imports System.Configuration
Imports MyClasses
Imports DAL
Imports System.Windows.Forms
Imports System.Drawing

'Divya 25/10/2007
Public Class frmCounterSettings

    Dim txtName As TextBox
    Dim btnName As Button
    Dim intCount As Integer
    Dim intcounter As Integer
    Dim objCounter(0) As CCounter
    Dim CounterValue(0) As String
    Dim strColor


    'Written By:-VINIT(2008/02/16)
    'Checking the text and tag of Controls
    Private Function isUpdate() As Boolean
        Dim dtName As New DataTable
        Try
            If Trim(txtCounters.Text) <> Trim(txtCounters.Tag) > 0 Then
                Return True
            End If
            If Convert.ToInt32(txtCounters.Text.ToString()) > 0 Then
                dtName = BCounter.GetCounterCaption()
                For intCount = 0 To Convert.ToInt32(txtCounters.Text.ToString()) - 1
                    txtName = CType(pnlCounters.Controls("txtName" & intCount), TextBox)
                    'If Trim(txtName.Text) <> Trim(CounterValue(intCount)) Then
                    '    Return True
                    'End If
                    If Trim(txtName.Text) <> Trim(dtName.Rows(intCount).Item("Caption")) Then
                        Return True
                    End If
                Next
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetData")
            Me.Cursor = Cursors.Default
            Return Nothing
        End Try
    End Function

    Private Sub frmCounterSettings_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            Me.Cursor = Cursors.WaitCursor
            If isUpdate() Then
                Select Case BCommon.ShowMsgError(10081, "YNC")
                    'Select Case MessageBox.Show(BCommon.GetMessage(10081), ConfigurationManager.AppSettings("MsgTitle"), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
                    Case Windows.Forms.DialogResult.Yes
                        If ValidateData() Then
                            Call SaveData()
                        Else
                            e.Cancel = True
                        End If
                    Case Windows.Forms.DialogResult.Cancel
                        e.Cancel = True
                    Case Windows.Forms.DialogResult.No
                End Select
            End If
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnClose_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub frmCounterSettings_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        'Rauhl_ITS@20073012 : Add following code display help
        Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Counter Settings")
        e.Cancel = True
    End Sub

    Private Sub frmCounterSettings_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        'Rauhl_ITS@20073012 : Add following code display help
        If e.KeyCode = Keys.F1 Then
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Counter Settings")
        End If
    End Sub

    Private Sub frmCounterSettings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call DisplayData()
        txtCounters.Tag = txtCounters.Text
    End Sub

    Private Sub ShowCounters()
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim dtGetData As New DataTable
            pnlCounters.Width = Me.Width - 50
            pnlCounters.BackColor = Me.BackColor

            If txtCounters.Text <> "" Then
                If ValidateTxtCounters() = False Then Exit Sub
                Call clearControls()

                For intCount = 0 To intcounter - 1
                    'Creating Dynamic TextBox and Assiging its properties
                    txtName = New TextBox()
                    btnName = New Button()

                    With btnName
                        Me.pnlCounters.BackgroundImageLayout = System.Windows.Forms.FlowDirection.TopDown
                        .Name = "btnName" & intCount
                        .Width = 29
                        .Height = 25
                        .Left = 8
                        If intCount > 0 Then .Top = .Top + 30 Else .Top = 10
                        .Text = Trim(intCount + 1)
                        .Visible = True
                        .BackColor = Color.AliceBlue
                        .Parent = pnlCounters
                        AddHandler pnlCounters.Controls("btnName" & intCount).Click, AddressOf Counter_Click

                        With txtName
                            .Name = "txtName" & intCount
                            .Width = 110
                            .Height = 30
                            .Left = btnName.Left + 10
                            .Top = btnName.Top
                            .MaxLength = 10
                            .Visible = True
                            .Parent = pnlCounters
                        End With
                    End With

                Next
                dtGetData = BCounter.GetData()
                If dtGetData.Rows.Count > 0 Then
                    If dtGetData.Rows.Count <= Trim(txtCounters.Text) Then
                        For intCount = 0 To dtGetData.Rows.Count - 1
                            strColor = Split(dtGetData.Rows(intCount).Item("ColorCode"), ",")
                            pnlCounters.Controls("txtName" & intCount).Text = dtGetData.Rows(intCount).Item("Caption")
                            pnlCounters.Controls("btnName" & intCount).BackColor = System.Drawing.Color.FromArgb(Convert.ToInt32(strColor(0)), Convert.ToInt32(strColor(1)), Convert.ToInt32(strColor(2)), Convert.ToInt32(strColor(3)))
                        Next
                    Else
                        For intCount = 0 To Trim(txtCounters.Text) - 1
                            strColor = Split(dtGetData.Rows(intCount).Item("ColorCode"), ",")
                            pnlCounters.Controls("txtName" & intCount).Text = dtGetData.Rows(intCount).Item("Caption")
                            pnlCounters.Controls("btnName" & intCount).BackColor = System.Drawing.Color.FromArgb(Convert.ToInt32(strColor(0)), Convert.ToInt32(strColor(1)), Convert.ToInt32(strColor(2)), Convert.ToInt32(strColor(3)))
                        Next
                    End If
                End If
            End If
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ShowCounters")
            Me.Cursor = Cursors.Default
        End Try
    End Sub
    Private Function ValidateTxtCounters()
        Dim intVar As Integer
        Try
            Me.Cursor = Cursors.WaitCursor
            intVar = Convert.ToInt32(txtCounters.Text)
            If intVar > 10 Then
                BCommon.ShowMsgError(10254)
                txtCounters.Text = ""
                txtCounters.Focus()
                Return False
            End If
            Me.Cursor = Cursors.Default
            Return True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ValidateTxtCounters")
            Me.Cursor = Cursors.Default
            Return Nothing
        End Try
    End Function
    Private Sub Counter_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Me.Cursor = Cursors.WaitCursor
            cdl.Color = sender.BackColor
            cdl.ShowDialog()
            sender.BackColor = cdl.Color
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Counter_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub txtCounters_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCounters.KeyDown
        Try
            If e.KeyCode = Keys.Tab Then Exit Sub
            If e.Shift Then e.SuppressKeyPress = True : Exit Sub
            If e.KeyCode = Keys.Enter Then SendKeys.Send("{TAB}")
            If BCommon.ValidateMiscell(e.KeyCode, "PHONE") = False Then
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtCounters_KeyDown")
        End Try
    End Sub

    Private Sub txtCounters_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCounters.TextChanged
        Call ShowCounters()
    End Sub
    'Private Sub txtCounters_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCounters.LostFocus
    '    Call ShowCounters()
    'End Sub

    Private Sub clearControls()
        Me.Cursor = Cursors.WaitCursor
        intcounter = Me.pnlCounters.Controls.Count
        If intcounter > 0 Then Me.pnlCounters.Controls.Clear()
        intcounter = Convert.ToInt32(txtCounters.Text)
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub DisplayData()
        Dim dtGetData As New DataTable
        Try
            Me.Cursor = Cursors.WaitCursor
            Call BCommon.SetFormStandards(Me)
            dtGetData = BCounter.GetData()
            If dtGetData.Rows.Count > 0 Then
                ReDim CounterValue(dtGetData.Rows.Count - 1)
                txtCounters.Text = dtGetData.Rows.Count

                For intCount = 0 To dtGetData.Rows.Count - 1
                    txtName = New TextBox()
                    btnName = New Button()
                    strColor = Split(dtGetData.Rows(intCount).Item("ColorCode"), ",")
                    'pnlCounters.Controls("txtName" & intCount).Text = dtGetData.Rows(intCount).Item("Caption")
                    'pnlCounters.Controls("btnName" & intCount).BackColor = System.Drawing.Color.FromArgb(Convert.ToInt32(strColor(0)), Convert.ToInt32(strColor(1)), Convert.ToInt32(strColor(2)), Convert.ToInt32(strColor(3)))
                    txtName.Text = dtGetData.Rows(intCount).Item("Caption")
                    txtName.Tag = txtName.Text
                    CounterValue(intCount) = txtName.Tag
                    btnName.BackColor = System.Drawing.Color.FromArgb(Convert.ToInt32(strColor(0)), Convert.ToInt32(strColor(1)), Convert.ToInt32(strColor(2)), Convert.ToInt32(strColor(3)))
                Next
            End If
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayData")
            Me.Cursor = Cursors.Default
        Finally
            dtGetData = Nothing
        End Try
    End Sub
    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Me.Cursor = Cursors.WaitCursor
        Call SaveData()
        txtCounters.Tag = txtCounters.Text
        Me.Cursor = Cursors.Default
    End Sub
    Private Sub SaveData()
        Dim blnResult As Boolean
        Try
            If ValidateData() = False Then Exit Sub
            blnResult = BCounter.Save(SetData())
            If blnResult Then BCommon.ShowMsgError(5) Else BCommon.ShowMsgError(4)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Function SetData() As CCounter()
        Dim txtValue As String = ""
        Dim colorBtn As Color
        ReDim objCounter(txtCounters.Text - 1)
        Try
            If Convert.ToInt32(txtCounters.Text.ToString()) > 0 Then
                For intCount = 0 To Convert.ToInt32(txtCounters.Text.ToString()) - 1
                    btnName = CType(pnlCounters.Controls("btnName" & intCount), Button)
                    colorBtn = btnName.BackColor '.FromArgb(red, green, blue)
                    txtName = CType(pnlCounters.Controls("txtName" & intCount), TextBox)
                    txtValue = txtName.Text.ToString()
                    objCounter(intCount) = New CCounter
                    objCounter(intCount).CounterID = intCount + 1
                    objCounter(intCount).OperatorID = intCount + 1
                    objCounter(intCount).Caption = txtValue
                    objCounter(intCount).ColuorCode = colorBtn.A & "," & colorBtn.R & "," & colorBtn.G & "," & colorBtn.B
                Next
            End If
            Return objCounter

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetData")
            Me.Cursor = Cursors.Default
            Return Nothing
        End Try
    End Function
    Public Function ValidateData() As Boolean
        Try
            Me.Cursor = Cursors.WaitCursor
        
            If txtCounters.Text = "" Then
                BCommon.ShowMsgError(10212)
                txtCounters.Focus()
                Return False
            End If
            If txtCounters.Text = 0 Then
                BCommon.ShowMsgError(10213)
                txtCounters.Text = ""
                txtCounters.Focus()
                Return False
            End If
            Return True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ValidateData")
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Private Sub btnCancel_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Cursor = Cursors.WaitCursor
        Call DisplayData()
        Me.Cursor = Cursors.Default
    End Sub
End Class
